package com.sry.blog.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sry.blog.enums.Exist;
import com.sry.blog.sys.mapper.UserMapper;
import com.sry.blog.sys.model.User;
import com.sry.blog.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB 服务实现类
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    /**
     * 修改信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean creatOrUpdate(User user) {
        return saveOrUpdate(user);
    }

    /**
     * 逻辑删除(实际是修改isdelete)
     *
     * @param idList
     * @return
     */
    @Override
    public boolean delete(List<Integer> idList) {
        return false;
    }


    /**
     * 分页查询
     *
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public Page<User> list(Integer pageNum,Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return page(page);
    }

    /**
     * 条件查询
     *
     * @param user
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public Page<User> lists(User user, Integer pageNum,Integer pageSize) {
        final String userName = user.getUserName();
        final String nickName = user.getNickName();
        final String phone = user.getPhone();
        final String email = user.getEmail();
        final String sex = user.getSex();
        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<User> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(userName)) {
            log.info(userName);
            lambda.like(User::getUserName, userName);
        }
        if (StrUtil.isNotEmpty(nickName)) {
            lambda.or().like(User::getNickName, nickName);
        }
        if (StrUtil.isNotEmpty(phone)) {
            lambda.or().eq(User::getPhone, phone);
        }
        if (StrUtil.isNotEmpty(email)) {
            lambda.or().eq(User::getEmail, email);
        }
        if (StrUtil.isNotEmpty(sex)) {
            lambda.or().eq(User::getSex, sex);
        }
        return page(page,wrapper);
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public User getOneById(Integer id) {
        return getById(id);
    }

    /**
     * 获取用户加密盐
     * <p>
     * 若没有用户自动返回null
     *
     * @param user
     * @return
     */
    @Override
    public User getUser(User user) {
        String userName = user.getUserName();
        String phone = user.getPhone();
        String email = user.getEmail();
        String password = user.getPassword();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<User> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(phone) || StrUtil.isNotEmpty(userName) || StrUtil.isNotEmpty(email)) {
            lambda.eq(User::getPhone, phone).or().eq(User::getEmail, email).or().eq(User::getUserName, userName);
        }
        return getOne(wrapper);
    }

    /**
     * @param user
     * @return
     */
    @Override
    public Exist isExist(User user) {
        Integer id = user.getId();
        String userName = user.getUserName();
        String phone = user.getPhone();
        String email = user.getEmail();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<User> lambda = wrapper.lambda();
        lambda.ne(null != id, User::getId, id);
        if (StrUtil.isNotEmpty(phone) || StrUtil.isNotEmpty(userName) || StrUtil.isNotEmpty(email)) {
            if (StrUtil.isNotEmpty(userName)) {
                lambda.eq(User::getUserName, userName);
                if (getOne(wrapper) != null) {
                    return Exist.USERNAME_EXIST;
                }
            } else if (StrUtil.isNotEmpty(phone)) {
                lambda.eq(User::getPhone, phone);
                if (getOne(wrapper) != null) {
                    return Exist.PHONE_EXIST;
                }
            } else if (StrUtil.isNotEmpty(email)) {
                lambda.eq(User::getEmail, email);
                if (getOne(wrapper) != null) {
                    return Exist.EMAIL_EXIST;
                }
            }
            return Exist.NONE_EXIST;
        }
        return null;

    }
}
